/**
 * 
 */
package co.fitstart.payment.entity.request;

import java.util.Date;

import com.demo2do.core.utils.DateFormatUtils;

import co.fitstart.payment.entity.app.App;
import co.fitstart.payment.entity.channel.Channel;

/**
 * 支付请求记录
 * 
 * @author lute
 */
public class RequestRecord {

    private Long id;
    
    private Long appId;
    
    private Long channelId;
    
    /** 唯一支付请求号 */
    private String requestNo;
    
    /** 订单号 */
    private String orderNo;
    
    /** 用户id */
    private String customerId;
    
    private String notifyUrl;
    
    /** 请求时间 */
    private Date requestTime;
    
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public RequestRecord() {
        
    }
    
    /**
     * @param app
     * @param channel
     * @param orderNo
     * @param customerId
     * @param notifyUrl
     */
    public RequestRecord(App app, Channel channel, String orderNo, String customerId, String notifyUrl) {
        this.appId = app.getId();
        this.channelId = channel.getId();
        this.orderNo = orderNo;
        this.customerId = customerId;
        this.notifyUrl = notifyUrl;
        this.requestTime = new Date();
        
        // Get the final request number, 24 digits in total
        this.requestNo = genRequestNo(app.getAppid(), channel.getName(), requestTime);
    }
    
    /**
     * Generate request number
     * 
     * @param appid
     * @param channelName
     * @param requestTime
     * @return
     */
    private String genRequestNo(String appid, String channelName, Date requestTime) {
        
        // Get appid abbreviation: first character + last character of appid
        String appidAbbreviation = new StringBuilder()
                                        .append(appid.charAt(0))
                                        .append(appid.charAt(appid.length() - 1))
                                        .toString()
                                        .toLowerCase();
        
        // Get channel name abbreviation: first character + last character of appid
        String channelNameAbbreviation = new StringBuilder()
                                            .append(channelName.charAt(0))
                                            .append(channelName.charAt(channelName.length() - 1))
                                            .toString()
                                            .toLowerCase();
        
        return new StringBuilder()
                    .append(appidAbbreviation)  // 2 digits appid abbreviation
                    .append(channelNameAbbreviation)  // 2 digits channel name abbreviation
                    .append("000")  // 3 placeholder digits for future possible usage
                    .append(String.format("%02d", Double.valueOf(Math.floor(Math.random() * 100)).intValue()))  // 2 digits random number between 0 (inclusive) ~ 100 (exclusive)
                    .append(DateFormatUtils.format(this.requestTime, "yyMMddHHmmssSSS"))  // 15 digits timestamp of request time, precise to milliseconds
                    .toString();
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the appId
     */
    public Long getAppId() {
        return appId;
    }

    /**
     * @return the channelId
     */
    public Long getChannelId() {
        return channelId;
    }

    /**
     * @return the requestNo
     */
    public String getRequestNo() {
        return requestNo;
    }

    /**
     * @return the orderNo
     */
    public String getOrderNo() {
        return orderNo;
    }

    /**
     * @return the customerId
     */
    public String getCustomerId() {
        return customerId;
    }

    /**
     * @return the notifyUrl
     */
    public String getNotifyUrl() {
        return notifyUrl;
    }

    /**
     * @return the requestTime
     */
    public Date getRequestTime() {
        return requestTime;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param appId the appId to set
     */
    public void setAppId(Long appId) {
        this.appId = appId;
    }

    /**
     * @param channelId the channelId to set
     */
    public void setChannelId(Long channelId) {
        this.channelId = channelId;
    }

    /**
     * @param requestNo the requestNo to set
     */
    public void setRequestNo(String requestNo) {
        this.requestNo = requestNo;
    }

    /**
     * @param orderNo the orderNo to set
     */
    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    /**
     * @param customerId the customerId to set
     */
    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    /**
     * @param notifyUrl the notifyUrl to set
     */
    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    /**
     * @param requestTime the requestTime to set
     */
    public void setRequestTime(Date requestTime) {
        this.requestTime = requestTime;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

}
